Include a header file required for build on mac 10.4
[supercollider.git] / Help / Tutorials / Getting-Started / Functions and Sound.html
blob128e4263978fa005f1e4c40e7bbdf52718c0970a
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.42">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000ff}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
20 span.s1 {color: #0019b7}
21 span.s2 {font: 9.0px Monaco}
22 span.s3 {font: 12.0px Helvetica}
23 span.s4 {color: #000000}
24 span.s5 {color: #0000ff}
25 span.Apple-tab-span {white-space:pre}
26 </style>
27 </head>
28 <body>
29 <p class="p1"><b>And What About Functions and Sound?</b></p>
30 <p class="p2"><br></p>
31 <p class="p3">I've probably bored you enough with technical details, so let's get back to making noise, which I assume is why you're reading this after all. Trust me though, all this work will pay off later, and believe it or not, we've already covered a fair amount of the basics of the language, at least in passing.</p>
32 <p class="p2"><br></p>
33 <p class="p3">Let's go back to our sound example, or rather a slightly simplified version of it. Check that the localhost server is<span class="Apple-converted-space">  </span>running, execute the code below and then press Cmd-. when you've had enough.</p>
34 <p class="p2"><br></p>
35 <p class="p4"><span class="Apple-tab-span"> </span>{ <span class="s1">SinOsc</span>.ar(440, 0, 0.2) }.play;</p>
36 <p class="p2"><br></p>
37 <p class="p3">In this case we've created a Function by enclosing some code in curly brackets, and then called the method 'play' on that Function. To Functions 'play' means evaluate yourself and play the result on a server. If you don't specify a server, you'll get the default one, which you'll recall is stored in the variable '<span class="s2">s</span>' and is set at startup to be the localhost server.<span class="Apple-converted-space"> </span></p>
38 <p class="p2"><br></p>
39 <p class="p3">We didn't store the Function in a variable, so it can't be reused. (Well, actually you could just execute the same line of code again, but you know what I mean...) This is often the case when using Function-play, as it is useful as a quick way of getting something to make noise, and is often used for testing purposes. There are other ways of reusing Functions for sounds, which are often better and more efficient as we will see.</p>
40 <p class="p2"><br></p>
41 <p class="p3">Lets look at what's between the curly brackets. We're taking something called a 'SinOsc' and we're sending it the message ar, with a few arguments. It turns out that SinOsc is an example of something called a <i>class</i>. To understand what a class is, we need to know a little more about OOP and objects.</p>
42 <p class="p2"><br></p>
43 <p class="p3">In a nutshell, an object is some data, i.e. some information, and a set of operations that you can perform on that data. You might have many different objects of the same type. These are called instances. The type itself is the object's class. For instance we might have a class called Student, and several instances of it, Bob, Dave and Sue. All three will have the same types of data, for instance they might have a bit of data named gpa. The value of each bit of data could be different however. They would also have the same methods to operate on the data. For instance they could have a method called calculateGPA, or something similar.</p>
44 <p class="p2"><br></p>
45 <p class="p3">An object's class defines its set of data (or <i>instance variables</i> as they are called) and methods. In addition it may define some other methods which only you send only to the class itself, and some data to be used by all of its instances. These are called class methods and class variables.</p>
46 <p class="p2"><br></p>
47 <p class="p3">All classes begin with upper-case letters, so it's pretty easy to identify them in code.</p>
48 <p class="p2"><br></p>
49 <p class="p3">Classes are what you use to make objects. They're like a template. You do this through class methods such as 'new', or, in the case of our SinOsc class above, 'ar'. Such methods return an object, an instance, and the arguments affect what its data will be, and how it will behave. Now take another look at the example in question:</p>
50 <p class="p2"><br></p>
51 <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s1">SinOsc</span>.ar(440, 0, 0.2)</p>
52 <p class="p5"><span class="Apple-tab-span"> </span></p>
53 <p class="p3">This tells the class SinOsc to make an instance of itself. All SinOscs are an example of what are called unit generators, or UGens. These are objects which produce audio or control signals. SinOsc is a sine wave oscillator. This means that it will produce a signal consisting of a single frequency. A graph of it's waveform would look like this:</p>
54 <p class="p2"><br></p>
55 <p class="p2"><br></p>
56 <p class="p3"><img src="attachments/Functions and Sound/Pasted Graphic.png" alt="attachments/Functions and Sound/Pasted Graphic.png"> <span class="Apple-tab-span"> </span>(don't worry about the 'index' and 'value' stuff; it's not important just now)</p>
57 <p class="p2"><br></p>
58 <p class="p2"><br></p>
59 <p class="p3">This waveform loops, creating the output signal.<span class="Apple-converted-space">  </span>'ar' means make the instance <i>audio rate</i>. SuperCollider calculates audio in groups of samples, called <i>blocks</i>. Audio rate means that the UGen will calculate a value for each sample in the block. There's another method, 'kr', which means <i>control rate</i>. This means calculate a single value for each block of samples. This can save a lot of computing power, and is fine for (you guessed it) signals which control other UGens, but it's not fine enough detail for synthesizing audio signals.</p>
60 <p class="p2"><br></p>
61 <p class="p3">The three arguments to SinOsc-ar given in the example determine a few things about the resulting instance. I happen to know that the arguments are frequency, phase, and mul. (We'll get to how I know that in a second.) Frequency is just the frequency of the oscillator in Hertz (Hz), or cycles per second (cps). Phase refers to where it will start in the cycle of its waveform. For SinOsc (but not for all UGens) phase is given in radians. If you don't know what radians are, don't worry, just understand that it's a value between 0 and 2 * pi. (You can look at a trigonometry text if you really want more detail.) So if we made a SinOsc with a phase of (pi * 0.5), or one quarter of the way through its cycle, the waveform would look like this:</p>
62 <p class="p2"><br></p>
63 <p class="p2"><br></p>
64 <p class="p6"><span class="s3"><img src="attachments/Functions and Sound/Pasted Graphic 1.png" alt="attachments/Functions and Sound/Pasted Graphic 1.png"></span></p>
65 <p class="p2"><br></p>
66 <p class="p2"><br></p>
67 <p class="p3">Make sense? Here are several cycles of the two side by side to make the idea clearer:</p>
68 <p class="p2"><br></p>
69 <p class="p2"><br></p>
70 <p class="p6"><span class="s3"><img src="attachments/Functions and Sound/Pasted Graphic 2.png" alt="attachments/Functions and Sound/Pasted Graphic 2.png"></span></p>
71 <p class="p2"><br></p>
72 <p class="p2"><br></p>
73 <p class="p3">So what about 'mul'? Mul is a special argument that almost all UGens have. It's so ubiquitous that it's usually not even explained in the documentation. It just means a value or signal by which the output of the UGen will be multiplied. It turns out that in the case of audio signals, this affects the amplitude of the signal, or how loud it is. The default mul of most UGens is 1, which means that the signal will oscillate between 1 and -1. This is a good default as anything bigger would cause clipping and distortion. A mul of 0 would be effectively silent, as if the volume knob was turned all the way down.</p>
74 <p class="p2"><br></p>
75 <p class="p3">To make clearer how mul works, here is a graph of two SinOscs, one with the default mul of 1, and one with a mul of 0.25:</p>
76 <p class="p2"><br></p>
77 <p class="p2"><br></p>
78 <p class="p6"><span class="s3"><img src="attachments/Functions and Sound/Pasted Graphic 3.png" alt="attachments/Functions and Sound/Pasted Graphic 3.png"></span></p>
79 <p class="p2"><br></p>
80 <p class="p2"><br></p>
81 <p class="p3">Get the idea? There's also another similar arg called 'add' (also generally unexplained in the doc), which (you guessed it) is something which is added to the output signal. This can be quite useful for things like control signals. 'add' has a default value of 0, which is why we don't need to specify something for it.</p>
82 <p class="p2"><br></p>
83 <p class="p3">Okay, with all this in mind, let's review our example, with comments:</p>
84 <p class="p2"><br></p>
85 <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span></span>(</p>
86 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>{ <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Open the Function</p>
87 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s1">SinOsc</span><span class="s4">.ar(<span class="Apple-tab-span"> </span></span>// Make an audio rate SinOsc</p>
88 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>440, <span class="Apple-tab-span"> </span></span>// frequency of 440 Hz, or the tuning A</p>
89 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0, <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// initial phase of 0, or the beginning of the cycle</p>
90 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.2) <span class="Apple-tab-span"> </span></span>// mul of 0.2</p>
91 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>}.play;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// close the Function and call 'play' on it</p>
92 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
93 <p class="p2"><br></p>
94 <p class="p8"><b>Some More Fun with Functions and UGens</b></p>
95 <p class="p2"><br></p>
96 <p class="p3">Here's another example of polymorphism, and how powerful it is. When creating Functions of UGens, for many arguments you don't have to use fixed values, you can in fact use other UGens! Below is an example which demonstrates this:</p>
97 <p class="p2"><br></p>
98 <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span></span>(</p>
99 <p class="p4"><span class="Apple-tab-span"> </span>{ <span class="s1">var</span> ampOsc;</p>
100 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>ampOsc = <span class="s1">SinOsc</span>.kr(0.5, 1.5pi, 0.5, 0.5);</p>
101 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">SinOsc</span>.ar(440, 0, ampOsc);</p>
102 <p class="p4"><span class="Apple-tab-span"> </span>}.play;</p>
103 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
104 <p class="p2"><br></p>
105 <p class="p3">Try this. (Again, use Cmd-. to stop the sound.)</p>
106 <p class="p2"><br></p>
107 <p class="p3">What we've done here is plugged the first SinOsc (a <i>control rate</i> one!) into the mul arg of the second one. So its output is being multiplied by the output of the second one. Now lets look at the first SinOsc's arguments.</p>
108 <p class="p2"><br></p>
109 <p class="p3">Frequency is set to 0.5 cps, which if you think about it a bit means that it will complete one cycle every 2 seconds. (1 / 0.5 = 2)</p>
110 <p class="p2"><br></p>
111 <p class="p3">Mul and add are both set to 0.5. Think for a second about what that will do. If by default SinOsc goes between 1 and -1, then a mul of 0.5 will scale that down to between 0.5 and -0.5. Adding 0.5 to that brings it to between 0 and 1, a rather good range for mul!</p>
112 <p class="p2"><br></p>
113 <p class="p3">The phase of 1.5pi (this just means 1.5 * pi) means 3/4 of the way through its cycle, which if you look at the first graph above you'll see is the lowest point, or in this case, 0. So the ampOsc SinOsc's waveform will look like this:</p>
114 <p class="p2"><br></p>
115 <p class="p2"><br></p>
116 <p class="p6"><span class="s3"><img src="attachments/Functions and Sound/Pasted Graphic 4.png" alt="attachments/Functions and Sound/Pasted Graphic 4.png"></span></p>
117 <p class="p2"><br></p>
118 <p class="p2"><br></p>
119 <p class="p3">And what we have in the end is a SinOsc that fades gently in and out. Shifting the phase just means that we start quiet and fade in. We're effectively using ampOsc as what is called an amplitude <i>envelope</i>. There are other ways of doing the same thing, some of them simpler, but this demonstrates the principal.</p>
120 <p class="p2"><br></p>
121 <p class="p3">Patching together UGens in this way is the basic way that you make sound in SC. For an overview of the various types of UGens available in SC, see <a href="../../UGens/UGens.html"><span class="s5">UGens</span></a> or <a href="../../UGens/Tour_of_UGens.html"><span class="s5">Tour_of_UGens</span></a>.</p>
122 <p class="p2"><br></p>
123 <p class="p3">For more information see:<b><span class="Apple-converted-space"> </span></b></p>
124 <p class="p2"><br></p>
125 <p class="p9"><a href="../../Language/Functions.html">Functions</a><span class="s4"><b> </b><a href="../../Core/Kernel/Function.html"><span class="s3">Function</span></a><b> </b><a href="../../UGens/UGens.html"><span class="s3">UGens</span></a> <a href="../../UGens/Tour_of_UGens.html"><span class="s3">Tour_of_UGens</span></a></span></p>
126 <p class="p2"><br></p>
127 <p class="p8"><b>Suggested Exercise:</b></p>
128 <p class="p10"><br></p>
129 <p class="p3">Experiment with altering the Functions in the text above. For instance try changing the frequencies of the SinOsc, or making multi-channel versions of things.</p>
130 <p class="p2"><br></p>
131 <p class="p3">____________________</p>
132 <p class="p2"><br></p>
133 <p class="p3">This document is part of the tutorial <b>Getting Started With SuperCollider</b>.</p>
134 <p class="p2"><br></p>
135 <p class="p3">Click here to go on to the next section: <a href="Presented in Living Stereo.html"><span class="s5">Presented in Living Stereo</span></a></p>
136 <p class="p2"><br></p>
137 <p class="p3">Click here to return to the table of Contents: <a href="Getting Started With SC.html"><span class="s5">Getting Started With SC</span></a></p>
138 </body>
139 </html>